<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/29 0029
 * Time: 11:40
 */

namespace app\admin\controller;


use think\Cache;
use think\Db;

class Transaction extends Middle
{
  
  
  	public function GetClMoeny(){
    	$defult = Db::name('common')->where(['id'=>30])->value('content');
      	$now = $this->redis->get('money');
      	return $this->returnSuccess(['default'=>$defult,'now'=>$now]);
    	
    }

  
  	public function ClMoneyReSet(){
    	$defult = Db::name('common')->where(['id'=>30])->value('content');
      	$now = $this->redis->set('money',$defult);
    	return $this->returnSuccess(null,'重置成功');
    }
  	
  	public function ClNumReSet(){
    	$res = Db::name('transaction')->where(['id'=>['<',99]])->update(['num'=>0]);
      	if($res)return $this->returnSuccess(null,'重置成功');
      	return $this->returnError('操作失败');
    }
  	
    //todo 数据交易对
    public function TranList(){
        $page = $this->page($this->data);
        $list = Db::name('trading_order')
            ->order('create_time desc')
            ->limit($page['page'],$page['limit'])
            ->select();
        $count = Db::name('trading_order')
            ->count();
      	$num = Db::name('transaction')->select();
        return $this->returnSuccess(['list'=>$list,'count'=>$count,'num'=>$num]);
    }

    //todo 交易数据设置
    public function TranCurrency(){
        return $this->returnSuccess(Cache::get('huobi'));
    }

    public function TranPay(){
        $money = $this->redis->get('money');
        $set = [
            'title' =>$this->data['title'],
            'create_time'  =>time(),
            'num'   =>$this->data['num'],
            'price' =>$this->data['price'],
            'total' =>round($this->data['num']*$this->data['price'],3),
            'type'  =>$this->data['type'],
          	'lv'	=>$this->data['lv'],
        ];
        Db::startTrans();
        try{
            switch ($this->data['type']){
                case 2;
                    if($set['total'] > $money)return $this->returnError('当前资金不足,无法购买');
                    $num = $this->data['num'];
                    $total = $set['total'];
                    Db::name('transaction')->where(['title'=>$this->data['title']])->setInc('num',$num);
                    $this->redis->set('money',$money-$total);
                    break;

                case 1;
                    $ciyou_num = Db::name('transaction')->where(['title'=>$this->data['title']])->value('num');
                    if($this->data['num'] > $ciyou_num)return $this->returnError('该币种持有量不足,无法卖出');
                    $num = $this->data['num'];
                    $total = $set['total'];
                    Db::name('transaction')->where(['title'=>$this->data['title']])->setDec('num',$num);
                    $this->redis->set('money',$money+$total);
                    break;
            }
            $res = Db::name('trading_order')->insert($set);
            if(!$res)return $this->returnError($this->Error);
            $user= Db::name('user_service')->where(['cl'=>1])->column('uid');
            if($user){
                $res = Db::name('message_model')->where(['type'=>6])->find();
                $set['type'] = $set['type'] == 1 ? '卖出' : '买入';
                $set['platform'] = 'Huobi';
                $msg = $this->GetMessage($res['content'],$res['title'],$set);
                $this->SendMsg($msg,$user,3);
                $reg_id = $this->GetPushReg($user);
                if($reg_id)$this->PushMsg($msg['content'],$msg['title'],$reg_id);
            }
            Db::commit();
            return $this->returnSuccess(null,'操作成功');
        }catch (\Exception $e){
            Db::rollback();
            return $this->returnError($e->getMessage());
        }
    }
}
